package com.phf.vhr.web.utils;

import com.phf.vhr.web.model.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 返回数据封装类
 *
 * @program: vhr-master
 * @description:
 * @author: Peng houfu
 * @create: 2021-09-26 13:38
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable  {

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 回显数据
     */
    private T data;

    /**
     * 未知响应封装
     * @return
     */
    public static Result success(){
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("SUCCESS");
        return result;
    }

    /**
     * 对象响应封装
     * @param object
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T object){
        Result<T> result = new Result<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setData(object);
        return result;
    }

    /**
     * 字符串响应封装
     * @param msg
     * @return
     */
    public static Result<String> success(String msg){
        Result<String> result = new Result<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg(msg);
        return result;
    }
    
    public static <T> Result<T> failure(String msg){
        Result<String> result = new Result<>();
        result.setCode(ResultEnum.ERROR_SERVER.getCode());
        result.setMsg(msg);
        return  failure(ResultEnum.ERROR_SERVER.getCode(), msg);
    }
    
    public static <T> Result<T> failure(Integer code, String msg){
        Result<T> result = new Result<>();
        result.setMsg(msg);
        result.setCode(code);
        result.setData(null);
        return result;
    }

    /**
     * 
     * @param resultEnum
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(ResultEnum resultEnum){
        return failure(resultEnum.getCode(), resultEnum.getMsg());
    }

    /**
     *  响应错误信息封装
     * @param code
     * @param data
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(Integer code, T data, String msg){
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return  result;
    }
    
    
    
}
